<?php
/**
 * Created by PhpStorm.
 * User: houju
 * Date: 2015/9/17
 * Time: 20:46
 */
namespace Admin\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function index(){
//    	$start = 
        $week = date('w');
        $start = date('Y-m-d',strtotime( '+'. 1-$week .' days' ));
        $teacher = M('user');
        $requirement = M('requirement');
        $order = M('order_form');
        $where = " addtime>=".$week." and type=1 and status = 0";
        $where1 = " addtime>=".$week." and type=2";
        $where2 = " date>=".$week." and verify=0";
        $where3 = " date>=".$week." and status=0";
        $data['teacher'] = $teacher->where($where)->count();
        $data['parents'] = $teacher->where($where1)->count();
        $data['requirement'] = $requirement->where($where2)->count();
        $data['order'] = $order->where($where3)->count();

        $this->assign('nav',"home");
        $this->assign('data',$data);

        $this -> display('index');
    }
    public function login(){
    	if (IS_POST){
    		if(!A('Pub/Code')->checkCode(I('post.code'))){
    			 $this->error('验证码错误');
    		};
    		$data['username'] = I('post.username');       
                $admin=M('admin')->where(array('username' => $data['username'], 'password' => md5(I('post.password'))))->find();
                if($admin=M('admin')->where($data)->find()){
    			session('admin',$admin['username']);
                        session('admin_id',$admin['id']);
    			$this->success('登录成功',U('index') );                        
    		}else{
    			$this->error('用户名或者密码不正确');
    		}
    	}else{
    		$this->display('login');
    	}
    }
    
    
    public function edit(){
        $error = '';
        if(IS_POST){
            $old_pass = I('post.old_pass');
            $new_pass = I('post.new_pass');
            $confirm_pass = I('post.confirm_pass');
            $admin_id = session('admin_id');
            $md5 = md5($old_pass);
            $admin = M('admin')->where(array('id' => $admin_id, 'password' => $md5))->find();
            if($admin){
                if($new_pass == $confirm_pass){
                    $admin->save(array('id' => $admin_id, 'password' => md5($new_pass)));
                    $error = '修改成功';
                }else{
                    $error = '两次输入密码不一致';
                }
            }else{
                $error = '原始密码不正确';
            }     
        }   
        $this->assign('error',$error);
        $this->assign('nav','passwd');
        $this->display('edit');
    }
    
    public function logout(){
        session('admin_id',null);
        session('admin',null);
        $this->redirect('/Admin/Index/index');
    }
    
    
}